/*
 * @(#)LuceneController.java, 2024/2/20 15:48
 *
 * Copyright (c) 2018-2024, 四川蜀天梦图数据科技有限公司.
 * All rights reserved.
 */
package spring.controller;

import controllers.Article;
import controllers.ArticleCleaner;
import controllers.EnglishDocumentIndexFactory;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.search.highlight.InvalidTokenOffsetsException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * LuceneController
 *
 * @author fantazy
 * @version 1.0, 2024年02月20日
 */
@Controller
public class LuceneController
{

    @Autowired
    private EnglishDocumentIndexFactory documentIndexFactory;

    @Autowired
    private ArticleCleaner articleCleaner;

    @RequestMapping(value = {"/", "index", "index.html"})
    public String index(Model model)
    {
        model.addAttribute("name", "zhoufan");
        return "index";
    }

    @RequestMapping(value = {"/search_table"})
    public String search_table(@RequestParam("query") String query, Model model)
    {
        try
        {
            System.out.println("Search keyword :" + query);
            model.addAttribute("articles", documentIndexFactory.searchDocuments(query));
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        catch (ParseException e)
        {
            e.printStackTrace();
        }

        return "search_table";
    }

    @RequestMapping("/search.json")
    @ResponseBody
    public Object search(@RequestParam("query") String query)
    {

        Map<String, String> ret = new HashMap<>();
        try
        {
            System.out.println("Search keyword :" + query);
            return documentIndexFactory.searchDocuments(query);
        }
        catch (IOException e)
        {
            ret.put("code", "500");
            ret.put("data", e.getMessage());
            e.printStackTrace();
            return ret;
        }
        catch (ParseException e)
        {
            ret.put("code", "500");
            ret.put("data", e.getCause().toString());
            e.printStackTrace();
            return ret;
        }
    }

    @RequestMapping("/write-index.json")
    @ResponseBody
    public Object writeIndex(@RequestParam("title") String title,
                             @RequestParam("content") String content)
    {
        content = articleCleaner.clean(content);
        System.out.printf("Title:%s\nCleaned Article:\n%s", title, content);
        Map<String, String> ret = new HashMap<>();
        try
        {
            documentIndexFactory.writeIntoLucene(title, content);
            ret.put("code", "200");
            ret.put("data", "ok");
        }
        catch (IOException e)
        {
            ret.put("code", "500");
            ret.put("data", e.getCause().toString());
            e.printStackTrace();
        }
        return ret;
    }

    @RequestMapping(value = {"article", "/article.html"})
    public String articleDetail(@RequestParam("no") Integer no,
                                @RequestParam("score") Float score,
                                @RequestParam("query") String query,

                                Model model)
    {
        try
        {
            final Article uniqueArticle = documentIndexFactory.findUniqueArticleViaTitle(no, query);
            model.addAttribute("score", score);
            model.addAttribute("title", uniqueArticle.getTitle());
            model.addAttribute("content", uniqueArticle.getContent());
        }
        catch (Throwable e)
        {
            e.printStackTrace();
            model.addAttribute("title", "Exceptionally");
            model.addAttribute("content", e.getMessage());
        }
        return "article";
    }
}
